package model.market;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import model.trader.MarketMaker;


public class MarketModel {

	public static void main(String[] args){

		 ModelResults runModel = runModel();
		writeFile(runModel.getResults());
		
		
	}

	private static ModelResults runModel() {
		
			MarketManager manager=new MarketManager();
			//AMLOW: trader builder..
			manager.getConfig().setTradeDelay(10);
			manager.getConfig().setNumberOfAssets(2);
//		manager.setJolt(1.3);
//			
//		manager.register(new NewsWatcher(0), new Position(50000));
//		manager.register(new NewsWatcher(1), new Position(50000));

		
				//manager.register(new MomentumExploiterInvestor(1),new Position(50000));
//			manager.register(new CapmAlphaHunter(),new Position(130000));
//			manager.register(new CapmAlphaHunter(),new Position(130000));
//			manager.register(new CapmAlphaHunter(),new Position(130000));
//				manager.register(new Accountant(1),new Position(130000));
//				manager.register(new Accountant(1),new Position(130000));
//				manager.register(new Accountant(1),new Position(130000));
//				manager.register(new Accountant(1),new Position(130000));
//				manager.register(new Accountant(1),new Position(130000));
//				manager.register(new Accountant(1),new Position(130000));
//				manager.register(new Accountant(1),new Position(130000));
//				manager.register(new Accountant(1),new Position(130000));
//				manager.register(new Accountant(1),new Position(130000));
//				manager.register(new Accountant(1),new Position(130000));
//				manager.register(new Accountant(1),new Position(130000));
//				manager.register(new Accountant(1),new Position(130000));
//				manager.register(new Accountant(1),new Position(130000));
//				manager.register(new Accountant(1),new Position(130000));
//				manager.register(new Accountant(1),new Position(130000));
				
				
				//manager.register(new MomentumTrader(1,3),new Position(200000));
				//manager.register(new MomentumTrader(1,4),new Position(200000));

				//manager.register(new MarketMaker(.003,200),new Position(30000000));
				manager.register(new MarketMaker(.005,500),new Position(30000000));
				manager.register(new MarketMaker(.01,1000),new Position(30000000));
				manager.register(new MarketMaker(.02,1000),new Position(30000000));
				manager.register(new MarketMaker(.03,1000),new Position(30000000));
				//manager.register(new MarketMaker(.04,1000),new Position(30000000));
				manager.register(new MarketMaker(.05,1000),new Position(30000000));
				//manager.register(new MarketMaker(.06,1000),new Position(30000000));
				manager.register(new MarketMaker(.07,1000),new Position(30000000));
				//manager.register(new MarketMaker(.08,1000),new Position(30000000));
				manager.register(new MarketMaker(.09,1000),new Position(30000000));
				manager.register(new MarketMaker(.1,2000),new Position(30000000));
				manager.register(new MarketMaker(.12,2000),new Position(30000000));
				manager.register(new MarketMaker(.14,3000),new Position(30000000));
				manager.register(new MarketMaker(.16,3000),new Position(30000000));
				manager.register(new MarketMaker(.18,3000),new Position(30000000));
				manager.register(new MarketMaker(.2,10000),new Position(30000000));
				manager.register(new MarketMaker(.25,15000),new Position(30000000));
				manager.register(new MarketMaker(.3,50000),new Position(30000000));
				manager.register(new MarketMaker(.4,120000),new Position(30000000));
				manager.register(new MarketMaker(.5,200000),new Position(30000000));
				manager.register(new MarketMaker(.65,200000),new Position(30000000));
				manager.register(new MarketMaker(.85,400000),new Position(30000000));
				//this guy is the buffer that stops our market blowin up..
				manager.register(new MarketMaker(1,1000000),new Position(30000000));
				//manager.register(new Opportunist(1.5,1000000),new Position(30000000));
				manager.register(new MarketMaker(2,10000000),new Position(30000000));
				manager.register(new MarketMaker(4,10000000),new Position(30000000));
				manager.register(new MarketMaker(6,100000000),new Position(30000000));
				
				
				try{
					return manager.execute();
				}catch(MarketException e){
					e.printStackTrace();
					return e.getStats();
				}
				
				
	}

	private static void writeFile(DayMarketInfo[] prices) {
		try {
			BufferedWriter writer=new BufferedWriter(
					new FileWriter("C:\\temp\\marketmodel\\Test"+System.currentTimeMillis()+".tsv"));
			writer.write("count");
			if(prices.length>0){
				for(int i=0;i<prices[0].getMarketPrices().length;i++){
					
					writer.write("	cashflow("+i+")	marketprice("+i+")	volume("+i+")");	
				}
			}
			
			writer.write("	tradersblown	tradersretired	totalworth	");
			//DayMarketInfo[] prices = manager.getPrices();
			List<Class> classes = new ArrayList<Class>();
			if(prices.length>0){
				Map<Class, Position> classPositions = prices[0].getClassPositions();
				Iterator<Class> keys = classPositions.keySet().iterator();
				while(keys.hasNext()){
					Class next =keys.next();
					writer.write(""+next.getName()+" worth	cash	quantity	");	
					classes.add(next);
				}
				writer.write("\n");
			}
			
			for(int i=0;i<prices.length;i++){
				DayMarketInfo info = prices[i];
				writer.write(""+i);
				for(int asset= 0;asset<info.getMarketPrices().length;asset++){
					writer.write("	"+info.getCashflowPerShare(asset)+
							"	"+ info.getMarketPrice(asset)+
							"	"+info.getVolume(asset));
					
				}
							
				writer.write("	"+info.getTradersBlown()+
							"	"+info.getTradersRetired()+
							"	"+info.getTotalWorth());
				Map<Class, Position> classPositions = info.getClassPositions();
				for(int classCount=0;classCount<classes.size();classCount++){
					Position position = classPositions.get(classes.get(classCount));
					
					if(position!=null){
						writer.write("	"+position.getWorth(info.getMarketPrices())+
									"	"+// position.getExecutedQuantity()+
									"	"+position.getCash());
					}else{
						writer.write("	0	0	0");
					}
				}
				writer.write("\n");
				
			}
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
